import socket
import re

# 图片
img_url = "https://i01piccdn.sogoucdn.com/00488d1c3d71e7fd"
client = socket.socket()
# 创立链接
client.connect(("i01piccdn.sogoucdn.com", 80))

# 构造请求报文
data = "GET /00488d1c3d71e7fd HTTP/1.1\r\nHost:i01piccdn.sogoucdn.com\r\n\r\n"

# 发送数据
client.send(data.encode())

# 接收数据
first_data = client.recv(1024)
print('first_data', first_data)

length = int(re.findall(b"Content-Length:(.*?)\r\n", first_data)[0])
print(length)
image_data = re.findall(b"\r\n\r\n(.*)", first_data, re.S)[0]

while 1:
    temp = client.recv(1024)
    image_data += temp
    if len(image_data) >= length:
        break
# 断开连接
client.close()

# 写入文件
with open("图片/car111.jpg", "wb") as f:
    f.write(image_data)
